package com.wangjw.leetcode;

/**
 * 超时错误
 *
 * Created by wangjw on 18/7/4.
 */
public class LeetCode307 {

    private int[] sum; // sum[i]存储前i个元素的和, sum[0] = 0

    private int[] data;

    public LeetCode307(int[] nums) {
        data = new int[nums.length];

        sum = new int[nums.length + 1];
        sum[0] = 0;
        for (int i = 1; i < sum.length; i++) {
            data[i - 1] = nums[i - 1];
            sum[i] = sum[i - 1] + nums[i - 1];
        }
    }

    public void update(int index, int val) {
        data[index] = val;
        for (int i = index + 1; i < sum.length; i++) {
            sum[i] = sum[i - 1] + data[i - 1];
        }
    }

    public int sumRange(int i, int j) {
        return sum[j + 1] - sum[i];
    }
}
